package com.zpf.web.servlet;


import com.zpf.pojo.Cart;
import com.zpf.pojo.Goods;
import com.zpf.pojo.User;
import com.zpf.service.CartService;
import com.zpf.service.GoodsService;
import com.zpf.service.impl.CartServiceImpl;
import com.zpf.service.impl.GoodServiceImpl;
import com.zpf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet{

    CartService cartService = new CartServiceImpl();


    //清空购物车
    public void  clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        cartService.clearCart(user.getId());

        response.getWriter().print("sessess");

    }


        //购物车数量+1或-1
    public void  addCartAjax(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");

        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //不存在则加，存在则更新
        // 根据商品id查询商品对象
        GoodsService goodsService = new GoodServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsId));

        //根据用户id和商品获取购物车信息
        Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        if(cart == null){
            //创建一个购物车对象
            cart = new Cart();
            //用户id
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsId));
            cart.setMoney(Integer.parseInt(number)*goods.getPrice());
            cart.setNum(Integer.parseInt(number));

            cartService.insert(cart);

        }else {
            //如果商品数量为0，则删除此记录
            if (Integer.parseInt(number) == 0) {
                cartService.deleteCart(user.getId(), Integer.parseInt(goodsId));
            }else{
                //修改商品的数量
                cart.setNum(cart.getNum() + Integer.parseInt(number));
                cart.setMoney(goods.getPrice() * cart.getNum());
                cartService.updateCart(cart);
            }
        }

        response.getWriter().print("success");
    }


        public String getCart(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            return"redirect:/login.jsp";
        }
        User u = (User) user;
        //获取当前用户的购物车(根据id)
        List<Cart> carts = cartService.findCartByUid(u.getId());
        //存储数据到请求域
        request.setAttribute("carts",carts);

        return"forward:/cart.jsp";

    }


    public String addCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object userObj = request.getSession().getAttribute("user");
        if(userObj == null){
            return "redirect:/login.jsp";
        }
        String goodsid = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StrUtils.isEmpty(goodsid) || StrUtils.isEmpty(number)){
            request.setAttribute("msg","添加购物车失败");
            return "forward:/message.jsp";
        }
        User user = (User) userObj;

        //根据商品id查询商品对象
        GoodsService goodsService =new GoodServiceImpl();
        Goods goods = goodsService.findById(Integer.parseInt(goodsid));

        //查询该用户有没有添加此商品到购物车，没有则新增，有则更新
        Cart cart = cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsid));
        if (cart == null) {
            //创建一个购物车对象
            cart = new Cart();
            //用户id
            cart.setId(user.getId());
            cart.setPid(Integer.parseInt(goodsid));
            cart.setMoney(Integer.parseInt(number)*goods.getPrice());
            cart.setNum(Integer.parseInt(number));

            cartService.insert(cart);

        }else{
        //此商品在购物车存在，则修改
            cart.setNum(cart.getNum()+Integer.parseInt(number));
//            cart.setMoney(new BigDecimal(goods.getPrice()).multiply(new BigDecimal(cart.getNum())).intValue());
            cart.setMoney(goods.getPrice() * cart.getNum());
            cartService.updateCart(cart);

        }


        return "redirect:/cartSuccess.jsp";

    }





    }
